package com.zhongkai.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateTool {
	public static java.util.Date getCurrentDate(String formatType) {
		Logger log = Logger.getLogger("DateTool");
		java.util.Date current_date = null;
		current_date = new java.util.Date();
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		String mydate = format.format(current_date);
		try {
			current_date = format.parse(mydate);
		} catch (java.text.ParseException ex) {
			log
					.info("DateTool.getCurrentDate(String formatType),ParseException:"
							+ ex.getMessage());
		} catch (Exception ex) {
			log.info("DateTool.getCurrentDate(String formatType),Exception:"
					+ ex.getMessage());
		}
		return current_date;
	}

	public static java.util.Date getCurrentDate() {
		Logger log = Logger.getLogger("DateTool");
		java.util.Date current_date = null;
		current_date = new java.util.Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		String mydate = format.format(current_date);
		try {
			current_date = format.parse(mydate);
		} catch (java.text.ParseException ex) {
			log.info("DateTool.getCurrentDate(),ParseException:"
					+ ex.getMessage());
		} catch (Exception ex) {
			log.info("DateTool,getCurrentDate(),Exception:" + ex.getMessage());
		}
		return current_date;
	}

	public static String formatDate(Date date2) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String mydate = "";
		if (date2 != null) {
			mydate = format.format(date2);
		}
		return mydate;
	}
	
	public static String formatDate(Date date2,String toFormat) {
		SimpleDateFormat format = new SimpleDateFormat(toFormat);
		String mydate = "";
		if (date2 != null) {
			mydate = format.format(date2);
		}
		return mydate;
	}
	
	public static Integer GetDate2Int() 
	{
		String DateString=formatDate(new java.util.Date());
		DateString=DateString.trim().replace("-", "");		
		return new Integer(DateString);
	}
	public static Integer GetDate2Int(String dateString) 
	{		
		dateString=dateString.trim().replace("-", "");		
		return new Integer(dateString);
	}
	public static Integer GetDate2Int(Date date) {		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateString=sdf.format(date);		
		dateString = dateString.trim().replace("-", "");
		return new Integer(dateString);
	}
	public static String get_format_db_dt(String date_format)
	{
		SimpleDateFormat simple_format = new SimpleDateFormat("yyyy-MM-dd");
		Date dt = getCurrentDate();
		simple_format = new SimpleDateFormat(date_format);
		String format_dt_str = simple_format.format(dt);
		dt = null;
		simple_format = null;
		return format_dt_str;
	}	
	 /**   
	    *   使用参数Format将字符串转为Date   
	    */   
	public   static   Date   parse(String strDate,String formatType) throws ParseException {   
	  return   strDate.equals("") ? null : new SimpleDateFormat(formatType).parse(strDate);   
	}   
	public static void main(String[] orgs){
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		Date dt = new Date();
		String date = sdf.format(dt);
		if(Integer.parseInt(date)==1)
			System.out.println("yeah");
		else
			System.out.println("fail");
		System.out.println(Integer.parseInt("01"));
	}
	
	/**
	 * 转入两个字符串类型的时间，时间的格式，和返回的相差类别
	 * @author 梁文敏
	 * @param type(nd=天数，nh=小时，nm=分钟，ns=秒钟)
	 * @throws ParseException 
	 * **/
	public static long dateDiff(String startTime, String endTime, String format, String type) throws ParseException {
		long value = 0;
		//按照传入的格式生成一个simpledateformate对象   
		SimpleDateFormat sd = new SimpleDateFormat(format);   
		long nd = 1000*24*60*60;//一天的毫秒数   
		long nh = 1000*60*60;//一小时的毫秒数   
		long nm = 1000*60;//一分钟的毫秒数   
		long ns = 1000;//一秒钟的毫秒数   
		long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();   //获得两个时间的毫秒时间差异
		if(type.equals("nd"))
			value = diff/nd;//计算差多少天   
		else if(type.equals("nh"))
			value = diff%nd/nh;//计算差多少小时   
		else if(type.equals("nm"))
			value = diff%nd%nh/nm;//计算差多少分钟   
		else if(type.equals("ns"))
			value = diff%nd%nh%nm/ns;//计算差多少秒
		return value;
	}
	
	/**
	 * 转入两个时间类型的时候，和返回的相差类别
	 * @author 梁文敏
	 * @param type(nd=天数，nh=小时，nm=分钟，ns=秒钟)
	 * @throws ParseException 
	 * **/
	public static long dateDiff(Date startTime, Date endTime, String type) throws ParseException {
		long value = 0;
		long nd = 1000*24*60*60;//一天的毫秒数   
		long nh = 1000*60*60;//一小时的毫秒数   
		long nm = 1000*60;//一分钟的毫秒数   
		long ns = 1000;//一秒钟的毫秒数   
		long diff = endTime.getTime() - startTime.getTime();   //获得两个时间的毫秒时间差异
		if(type.equals("nd"))
			value = diff/nd;//计算差多少天   
		else if(type.equals("nh"))
			value = diff%nd/nh;//计算差多少小时   
		else if(type.equals("nm"))
			value = diff%nd%nh/nm;//计算差多少分钟   
		else if(type.equals("ns"))
			value = diff%nd%nh%nm/ns;//计算差多少秒
		return value;
	}
	
	/**
	 * 得到传入月份的最后一天
	 * @author 梁文敏
	 * @param date传入的日期
	 * @throws ParseException 
	 * **/
	public static Date setMonthToLastDay(Date date) {      
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.set(Calendar.DAY_OF_MONTH, calendar      
	            .getActualMaximum(Calendar.DAY_OF_MONTH));      
	    return calendar.getTime();      
	} 
}
